using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Enum;
using GameCode;
using Manager;
using SO;
using Struct;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

namespace Entity {

    public partial class EntityController {

        private FightProperties fightProperties;
        public FightProperties FightProperties => fightProperties;

        public CharacterCanvas CharacterCanvas;


        /// <summary>
        /// 是否存活 存活返回true 否则返回false
        /// </summary>
        public bool IsAlive => GetHp() > 0;


        public void OnInitFightProperties(FightProperties fightProperties) {
            this.fightProperties = fightProperties;

        }

        public virtual int GetPropertiesValue(EnumPropertiesType type) {
            return FightProperties.GetProperties(type).value;
        }


        public virtual int GetHp() {
            return GetPropertiesValue(EnumPropertiesType.HP);
        }


        public virtual int GetMaxHp() {
            return GetPropertiesValue(EnumPropertiesType.MaxHp);
        }


        public virtual int GetMp() {
            return GetPropertiesValue(EnumPropertiesType.MP);
        }


        public virtual int GetMaxMp() {
            return GetPropertiesValue(EnumPropertiesType.MaxMP);
        }


        public virtual void UpdateHealthSlider() {
            CharacterCanvas.UpdateHealthSlider(this);
        }


        public async virtual UniTask TakeDamage(int value) {
            //受伤先检测是否有护盾 若有护盾则减少护盾值 若护盾值不足则减少伤害值

            int shieldValue = FightProperties.GetProperties(EnumPropertiesType.Shield).value;
            //如果护盾大于扣除伤害值 则扣除护盾值 否则扣除 护盾值减伤害值
            if (shieldValue > 0) {
                //判断护盾值是否能够完全抵扣伤害值
                if (shieldValue >= value) {
                    //如果可以完全扣除
                    FightProperties.AddProperty(EnumPropertiesType.Shield, -value);
                    value = 0;
                } else {
                    //如果不能完全扣除 则扣除护盾值 并将剩余伤害值减去护盾值
                    FightProperties.AddProperty(EnumPropertiesType.Shield, shieldValue);
                    value -= shieldValue;
                }

            }

            if (value != 0) {
                //扣除血量
                FightProperties.AddProperty(EnumPropertiesType.HP, -value);
                if (!IsAlive) {
                    await Die();
                }

                GameLayerEffectManager.Instance.ShowDamageTipAnimation($"-{value}", GetShowTipPosition());
            }

            UpdateHealthSlider();
        }


        public async virtual void HealHP(int value) {
            FightProperties.AddProperty(EnumPropertiesType.HP, value, false, true);
            UpdateHealthSlider();
            if (!IsAlive) {
                await Die();
            }
        }


        public virtual void HealMP(int value) {
            FightProperties.AddProperty(EnumPropertiesType.MP, value, false, true);
            UpdateHealthSlider();
        }


        public async virtual UniTask Die() {
            bool checkSkillTrigger = await GameLayer.Instance.CheckSkillTrigger(EnumItemCheckTiming.SelfDieTrigger,this);
            if (!checkSkillTrigger) {
                ChangeState(EnumCharacterState.Die);
            }


        }


        public virtual bool HpIsFull() {
            return GetHp() >= GetMaxHp();
        }


        public virtual bool MpIsFull() {
            int maxMp = GetMaxMp();
            if (maxMp == 0) {
                return false;
            }

            return GetMp() >= maxMp;
        }


    }

}